﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Phone.Controls;

namespace Kiva7.Controls
{
    public class OrientedPivot : Pivot
    {

        #region

        public OrientedPivot()
        {
            this.Loaded += OrientedGridLoaded;
        }

        protected PhoneApplicationPage Page
        {
            get
            {
                return GetDependencyObjectFromVisualTree(this, typeof(PhoneApplicationPage)) as PhoneApplicationPage;
            }
        }

        protected DependencyObject GetDependencyObjectFromVisualTree(DependencyObject startObject, Type type)
        {
            DependencyObject parent = startObject;
            while (parent != null)
            {
                if (type.IsInstanceOfType(parent))
                    return parent;
                parent = VisualTreeHelper.GetParent(parent);
            }
            return null;
        }

        protected FrameworkElement FindByName(DependencyObject startObject, string name)
        {
            for(int i=0,
                j = VisualTreeHelper.GetChildrenCount(startObject); 
                i<j; i++)
            {
                DependencyObject dp = VisualTreeHelper.GetChild(startObject, i);
                FrameworkElement fe = dp as FrameworkElement;
                if (fe != null
                    && fe.Name == name)
                {
                    return fe;
                }
                fe = FindByName(dp, name);
                if (fe!= null)
                {
                    return fe;
                }
            }
            return null;
        }

        private void OrientedGridLoaded(object sender, RoutedEventArgs e)
        {
            PhoneApplicationPage page = Page;
            if (page != null)
            {
                ProcessOrientation(page.Orientation);
                page.OrientationChanged += PageOrientationChanged;
            }
        }

        private void PageOrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            ProcessOrientation(e.Orientation);
        }

        #endregion

        protected  void ProcessOrientation(PageOrientation orientation)
        {
            if ((orientation & PageOrientation.Landscape) == PageOrientation.Landscape)
            {
                SetLandscape(this);
            }
            else
            {
                SetPortrait(this);
            }
        }

        private void SetLandscape(Pivot pivot)
        {
            Grid grid = FindByName(pivot, "pivotGrid") as Grid;
            if (grid != null)
            {
                //grid.RowDefinitions.Clear();
                //grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Auto)});

                FrameworkElement pivotTitle = grid.FindName("pivotTitle") as FrameworkElement;
                if (pivotTitle != null)
                {
                    pivotTitle.Visibility = Visibility.Collapsed;
                }
                
                ItemsPresenter pivotItemPresenter = grid.FindName("PivotItemPresenter") as ItemsPresenter;
                if (pivotItemPresenter != null)
                {
                    Grid.SetRow(pivotItemPresenter, 0);
                    Grid.SetRowSpan(pivotItemPresenter, 3);
                    Grid.SetColumn(pivotItemPresenter, 1);
                    Grid.SetColumnSpan(pivotItemPresenter, 1);
                }

                Grid headersListGrid = grid.FindName("HeadersListGrid") as Grid;
                if (headersListGrid != null)
                {
                    Grid.SetRow(headersListGrid, 0);
                    Grid.SetRowSpan(headersListGrid, 3);
                    Grid.SetColumn(headersListGrid, 0);
                    Grid.SetColumnSpan(headersListGrid, 1);

                    headersListGrid.VerticalAlignment = VerticalAlignment.Bottom;
                    headersListGrid.Projection = new PlaneProjection()
                    {
                        RotationZ = 90
                    };
                }
            }

        }

        private void SetPortrait(Pivot pivot)
        {
            Grid grid = FindByName(pivot, "pivotGrid") as Grid;
            if (grid != null)
            {
                FrameworkElement pivotTitle = grid.FindName("pivotTitle") as FrameworkElement;
                if (pivotTitle != null)
                {
                    pivotTitle.Visibility = Visibility.Visible;
                }
                ItemsPresenter pivotItemPresenter = grid.FindName("PivotItemPresenter") as ItemsPresenter;
                if (pivotItemPresenter != null)
                {
                    Grid.SetRow(pivotItemPresenter, 2);
                    Grid.SetRowSpan(pivotItemPresenter, 1);
                    Grid.SetColumn(pivotItemPresenter, 0);
                    Grid.SetColumnSpan(pivotItemPresenter, 3);
                }

                Grid headersListGrid = grid.FindName("HeadersListGrid") as Grid;
                if (headersListGrid != null)
                {
                    Grid.SetRow(headersListGrid, 1);
                    Grid.SetRowSpan(headersListGrid, 1);
                    Grid.SetColumn(headersListGrid, 0);
                    Grid.SetColumnSpan(headersListGrid, 3);

                    headersListGrid.VerticalAlignment = VerticalAlignment.Top;
                    headersListGrid.Projection = null;
                }
            }

        }
    }
}
